package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public SysResult upload(MultipartFile file){
        ImageVO imageVO = fileService.upload(file);
        if (imageVO == null){
            SysResult.fail();
        }
        return SysResult.success(imageVO);

    }

//    public SysResult dupload(MultipartFile file) throws IOException {
//        // 1.获取文件名称
//        String fileName = file.getOriginalFilename();
//        // 2.准备文件目录
//        String dir = "F:/hx/project3/images";
//        File dirFile = new File(dir);
//        if (!dirFile.exists()) {
//            dirFile.mkdirs();
//        }
//        String path = dir + "/" + fileName;
//        // 实现文件上传
//        file.transferTo(new File(path));
//        return SysResult.success();
//    }
    // 文件删除
    @DeleteMapping("/deleteFile")
    @Transactional
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }
}
